package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcRfidReaderGroup;
import com.csun.cmny.provider.model.dto.rfid.RfidReaderGroupDto;
import com.csun.cmny.provider.model.dto.rfid.RfidReaderGroupQueryDto;
import com.csun.cmny.provider.model.vo.rfid.DmcRfidReaderVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcRfidReaderGroupBindService;
import com.csun.cmny.provider.service.DmcRfidReaderGroupService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-08-22 11:43
 * @Description: the class rfid reader group controller
 */
@RestController
@RequestMapping(value = "/rfidReaderGroup", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcRfidReaderGroupController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcRfidReaderGroupService dmcRfidReaderGroupService;
    @Resource
    private DmcRfidReaderGroupBindService dmcRfidReaderGroupBindService;

    /**
     * 查询没有绑定门禁组的门禁读卡器
     *
     * 注意：门禁读卡器需要先绑定位置
     *
     * @return  the wrapper
     */
    @RequestMapping(value = "/queryNotBindRfidReader", method = RequestMethod.POST)
    public Wrapper queryNotBindRfidReader() {

        List<DmcRfidReaderVo> dmcRfidReaderVoList = dmcRfidReaderGroupBindService
                .queryNotBindRfidReader(getLoginAuthDto().getGroupId());

      //  return WrapMapper.ok(dmcRfidReaderVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),dmcRfidReaderVoList);
    }


    /**
     * 查询没有绑定报警灯的门禁组
     *
     * @return the wrapper
     */
    @RequestMapping(value = "/queryNotBindAlarmLight", method = RequestMethod.POST)
    public Wrapper queryNotBindAlarmLight() {

        List<DmcRfidReaderGroup> dmcRfidReaderGroupList = dmcRfidReaderGroupService
                .queryNotBindAlarmLight(getLoginAuthDto().getGroupId());

       // return WrapMapper.ok(dmcRfidReaderGroupList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),dmcRfidReaderGroupList);
    }

    /**
     * 添加门禁组 （绑定门禁组和门禁读卡器）
     *
     * @param rfidReaderGroupDto    the rfid reader group dto
     *
     * @return  the wrapper
     */
    @RequestMapping(value = "/addWithRfidReader", method = RequestMethod.POST)
    public Wrapper addWithRfidReader(@RequestBody RfidReaderGroupDto rfidReaderGroupDto) {

        Integer result = dmcRfidReaderGroupService.addWithRfidReader(rfidReaderGroupDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 修改门禁组 （可以跟换绑定的门禁读卡器）
     *
     * @param rfidReaderGroupDto    the rfid reader group dto
     *
     * @return  the wrapper
     */
    @RequestMapping(value = "/editWithRfidReader", method = RequestMethod.POST)
    public Wrapper editWithRfidReader(@RequestBody RfidReaderGroupDto rfidReaderGroupDto) {

        Integer result = dmcRfidReaderGroupService.editWithRfidReader(rfidReaderGroupDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 删除门禁组及门禁读卡器绑定关系
     *
     * @param id    the rfid reader group id
     *
     * @return  the wrapper
     */
    @RequestMapping(value = "/deleteWithRfidReaderById/{id}", method = RequestMethod.POST)
    public Wrapper deleteWithRfidReaderById(@PathVariable Long id) {

        Integer result = dmcRfidReaderGroupService.deleteWithRfidReaderById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 查询门禁组列表 - 分页查询
     *
     * @param rfidReaderGroupQueryDto   the rfid reader group query dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryListWithPage(@RequestBody RfidReaderGroupQueryDto rfidReaderGroupQueryDto) {

        rfidReaderGroupQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        List<RfidReaderGroupDto> rfidReaderGroupDtoList = dmcRfidReaderGroupService
                .queryListWithPage(rfidReaderGroupQueryDto);

        //return WrapMapper.ok(new PageInfo<>(rfidReaderGroupDtoList));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),new PageInfo<>(rfidReaderGroupDtoList));
    }

}
